package com.im.file.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.im.base.CommonDAO;
import com.im.file.UploadFile;
import com.im.file.bean.FileData;
import com.im.file.bean.GroupHeadData;
import com.im.file.bean.UserHeadData;
import com.im.file.component.UploadComponent;
import com.only.common.util.OnlyDateUtil;
import com.only.common.util.OnlyPropertiesUtil;

/**
 * 描述：
 * 
 * @author XiaHui
 * @date 2015年12月30日 下午8:59:57
 * @version 0.0.1
 */
@Service
@Transactional
public class HeadService {

	private String upload = "upload/";

	@Autowired
	CommonDAO commonDAO;
	@Autowired
	UploadComponent uploadComponent;

	public List<String> userUpload(HttpServletRequest request, String userId) {

		String basePath = request.getSession().getServletContext().getRealPath("/");

		String path = OnlyPropertiesUtil.getPropertyByClassPtah("/setting/config.properties", "file.save.path");

		StringBuilder rootPath = new StringBuilder((StringUtils.isBlank(path)) ? basePath : path);// 拼接文件上传目录
		rootPath.append(upload + "user/");

		StringBuilder nodePath = new StringBuilder("");
		nodePath.append(OnlyDateUtil.getCurrentYear());
		nodePath.append("/");
		nodePath.append(OnlyDateUtil.getCurrentMonth());
		nodePath.append("/");
		nodePath.append(OnlyDateUtil.getCurrentDay());
		nodePath.append("/");
		UploadFile uploadFile = new UploadFile();
		List<FileData> fileDataList = uploadFile.uploadByIo(request, rootPath.toString(), nodePath.toString(), userId);
		return addUserHead(fileDataList, userId);
	}

	private List<String> addUserHead(List<FileData> fileDataList, String userId) {
		List<String> list = new ArrayList<String>();
		if (null != fileDataList && !fileDataList.isEmpty()) {
			for (FileData fd : fileDataList) {
				UserHeadData uhd = new UserHeadData();
				uhd.setName(fd.getName());
				uhd.setFullName(fd.getFullName());
				uhd.setSaveName(fd.getSaveName());
				uhd.setSaveFullName(fd.getSaveFullName());

				uhd.setRootPath(fd.getRootPath());
				uhd.setNodePath(fd.getNodePath());
				uhd.setSuffixName(fd.getSuffixName());
				uhd.setSize(fd.getSize());
				uhd.setUploadDate(new Date());
				uhd.setUserId(userId);

				commonDAO.save(uhd);
				list.add(uhd.getId());
			}
		}
		return list;
	}

	public List<String> groupUpload(HttpServletRequest request, String groupId) {

		String basePath = request.getSession().getServletContext().getRealPath("/");

		String path = OnlyPropertiesUtil.getPropertyByClassPtah("/setting/config.properties", "file.save.path");

		StringBuilder rootPath = new StringBuilder((StringUtils.isBlank(path)) ? basePath : path);// 拼接文件上传目录
		rootPath.append(upload + "group/");

		StringBuilder nodePath = new StringBuilder("");
		nodePath.append(OnlyDateUtil.getCurrentYear());
		nodePath.append("/");
		nodePath.append(OnlyDateUtil.getCurrentMonth());
		nodePath.append("/");
		nodePath.append(OnlyDateUtil.getCurrentDay());
		nodePath.append("/");
		UploadFile uploadFile = new UploadFile();
		List<FileData> fileDataList = uploadFile.uploadByIo(request, rootPath.toString(), nodePath.toString(), groupId);
		return addGroupHead(fileDataList, groupId);
	}

	private List<String> addGroupHead(List<FileData> fileDataList, String groupId) {
		List<String> list = new ArrayList<String>();
		if (null != fileDataList && !fileDataList.isEmpty()) {
			for (FileData fd : fileDataList) {
				GroupHeadData ghd = new GroupHeadData();
				ghd.setName(fd.getName());
				ghd.setFullName(fd.getFullName());
				ghd.setSaveName(fd.getSaveName());
				ghd.setSaveFullName(fd.getSaveFullName());

				ghd.setRootPath(fd.getRootPath());
				ghd.setNodePath(fd.getNodePath());
				ghd.setSuffixName(fd.getSuffixName());
				ghd.setSize(fd.getSize());
				ghd.setUploadDate(new Date());
				ghd.setGroupId(groupId);

				commonDAO.save(ghd);
				list.add(ghd.getId());
			}
		}
		return list;
	}

	public UserHeadData getUserHeadData(String id) {
		return this.commonDAO.get(UserHeadData.class, id);
	}

	public GroupHeadData getGroupHeadData(String id) {
		return this.commonDAO.get(GroupHeadData.class, id);
	}
}
